

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Blue~u~u~u">
  <meta name="author" content="Blue~u~u~u">
  <meta name="keywords" content="">
  <meta name="description" content="spring-day01第一章 Spring的简介第一节 Spring公司简介该公司的创建者Rod Johnson被称之为Spring之父，他领导的Spring研发团队下有众多的优秀开发者，Spring公司旗下有非常多的优秀框架。例如:Spring FrameWork、Spring Boot、Spring Cloud、Spring Data、Spring Security等等，几乎涉及了Java开">
<meta property="og:type" content="article">
<meta property="og:title" content="spring-day01-XML">
<meta property="og:url" content="http://www.slx.blue/2021/12/04/spring-day01/index.html">
<meta property="og:site_name" content="Blue~u~u~u~u">
<meta property="og:description" content="spring-day01第一章 Spring的简介第一节 Spring公司简介该公司的创建者Rod Johnson被称之为Spring之父，他领导的Spring研发团队下有众多的优秀开发者，Spring公司旗下有非常多的优秀框架。例如:Spring FrameWork、Spring Boot、Spring Cloud、Spring Data、Spring Security等等，几乎涉及了Java开">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_001.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_002.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_003.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_004.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_006.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_007.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_009.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_010.png">
<meta property="article:published_time" content="2021-12-04T05:45:19.000Z">
<meta property="article:modified_time" content="2021-12-06T06:38:04.543Z">
<meta property="article:author" content="Blue~u~u~u">
<meta property="article:tag" content="spring">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://www.slx.blue/2021/12/04/spring-day01/tu_001.png">
  
  <title>spring-day01-XML - Blue~u~u~u~u</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.slx.blue","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":180,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/>
<script src="/live2d-widget/autoload.js"></script>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Blue~u~u</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="spring-day01-XML">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-12-04 13:45" pubdate>
        2021年12月4日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      36k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      114 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">spring-day01-XML</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：6 个月前
                
              </p>
            
            <div class="markdown-body">
              <h1 id="spring-day01"><a href="#spring-day01" class="headerlink" title="spring-day01"></a>spring-day01</h1><h2 id="第一章-Spring的简介"><a href="#第一章-Spring的简介" class="headerlink" title="第一章 Spring的简介"></a>第一章 Spring的简介</h2><h3 id="第一节-Spring公司简介"><a href="#第一节-Spring公司简介" class="headerlink" title="第一节 Spring公司简介"></a>第一节 Spring公司简介</h3><p>该公司的创建者Rod Johnson被称之为Spring之父，他领导的Spring研发团队下有众多的优秀开发者，Spring公司旗下有非常多的优秀框架。例如:Spring FrameWork、Spring Boot、Spring Cloud、Spring Data、Spring Security等等，几乎涉及了Java开发的每一个领域。 官网地址：<a target="_blank" rel="noopener" href="https://spring.io/">https://spring.io/</a> </p>
<p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_001.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="第二节-Spring-Framework的介绍"><a href="#第二节-Spring-Framework的介绍" class="headerlink" title="第二节 Spring Framework的介绍"></a>第二节 Spring Framework的介绍</h3><h4 id="1-概念"><a href="#1-概念" class="headerlink" title="1. 概念"></a>1. 概念</h4><p>Spring Framework是Spring 基础框架，可以视为 Spring 基础设施，基本上任何其他 Spring 项目都是以 Spring Framework 为基础的。是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架</p>
<h4 id="2-特征"><a href="#2-特征" class="headerlink" title="2. 特征"></a>2. 特征</h4><ul>
<li>非侵入式：使用 Spring Framework 开发应用程序时，Spring 对应用程序本身的结构影响非常小。对领域模型(domain)可以做到零污染；对功能性组件也只需要使用几个简单的注解进行标记，完全不会破坏原有结构，反而能将组件结构进一步简化。这就使得基于 Spring Framework 开发应用程序时结构清晰、简洁优雅。</li>
<li>控制反转：IOC——Inversion of Control，反转资源获取方向。把自己创建资源变成环境将资源准备好，我们享受资源注入。</li>
<li>面向切面编程：AOP——Aspect Oriented Programming，在不修改源代码的基础上增强代码功能。</li>
<li>容器：Spring IOC 是一个容器，因为它包含并且管理组件对象的生命周期。组件享受到了容器化的管理，替程序员屏蔽了组件创建过程中的大量细节，极大的降低了使用门槛，大幅度提高了开发效率。</li>
<li>组件化：Spring 实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用 XML 和 Java 注解组合这些对象。这使得我们可以基于一个个功能明确、边界清晰的组件有条不紊的搭建超大型复杂应用系统。</li>
<li>声明式：很多以前需要编写代码才能实现的功能，现在只需要声明需求即可由框架代为实现。</li>
<li>一站式：在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库。而且 Spring 旗下的项目已经覆盖了广泛领域，很多方面的功能性需求可以在 Spring Framework 的基础上全部使用 Spring 来实现。</li>
</ul>
<h4 id="2-Spring-Framework五大功能模块"><a href="#2-Spring-Framework五大功能模块" class="headerlink" title="2. Spring Framework五大功能模块"></a>2. Spring Framework五大功能模块</h4><p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_002.png" srcset="/img/loading.gif" lazyload></p>
<table>
<thead>
<tr>
<th>功能模块</th>
<th>功能介绍</th>
</tr>
</thead>
<tbody><tr>
<td>Core Container</td>
<td>核心容器，在 Spring 环境下使用任何功能都必须基于 IOC 容器。</td>
</tr>
<tr>
<td>AOP&amp;Aspects</td>
<td>面向切面编程</td>
</tr>
<tr>
<td>Test</td>
<td>提供了对 junit 或 TestNG 测试框架的整合。</td>
</tr>
<tr>
<td>Data Access/Integration</td>
<td>提供了对数据访问/集成的功能。</td>
</tr>
<tr>
<td>Spring MVC</td>
<td>提供了面向Web应用程序的集成功能。</td>
</tr>
</tbody></table>
<h2 id="第二章-IOC容器概念"><a href="#第二章-IOC容器概念" class="headerlink" title="第二章 IOC容器概念"></a>第二章 IOC容器概念</h2><h3 id="第一节-容器的概念"><a href="#第一节-容器的概念" class="headerlink" title="第一节 容器的概念"></a>第一节 容器的概念</h3><h4 id="1-普通容器"><a href="#1-普通容器" class="headerlink" title="1. 普通容器"></a>1. 普通容器</h4><p>普通容器只是负责存储数据(对象)，例如我们在JavaSE中学习的数组、List、Map等等，可以让我们使用它存储数据、获取数据，不具备其它复杂的功能</p>
<h4 id="2-复杂容器"><a href="#2-复杂容器" class="headerlink" title="2. 复杂容器"></a>2. 复杂容器</h4><p>复杂容器不仅要负责存储对象，还需要具备创建对象、调用对象方法、管理对象生命周期、并且在一定情况下负责销毁对象。例如我们之前学习的Tomcat就是一个复杂容器，它能够负责创建Servlet、Filter、Listener等等对象，并且管理他们的生命周期，在生命周期的不同阶段调用他们的不同方法。而我们后续要学习的IOC容器也是一个复杂容器</p>
<h3 id="第二节-IOC的概念"><a href="#第二节-IOC的概念" class="headerlink" title="第二节 IOC的概念"></a>第二节 IOC的概念</h3><h4 id="1-传统方式创建对象"><a href="#1-传统方式创建对象" class="headerlink" title="1. 传统方式创建对象"></a>1. 传统方式创建对象</h4><p>传统方式创建对象的方式是: 需要哪个类的对象，就直接在项目中new哪个类的对象，这样就会导致各个类之间的耦合度非常高</p>
<p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_003.png" srcset="/img/loading.gif" lazyload></p>
<h4 id="2-IOC方式创建对象"><a href="#2-IOC方式创建对象" class="headerlink" title="2. IOC方式创建对象"></a>2. IOC方式创建对象</h4><p>IOC(inversion of control)的中文解释是“控制反转”，对象的使用者不是创建者.  作用是将对象的创建反转给spring框架来创建和管理。控制反转怎么去理解呢。 其实它反转的是什么呢，是对象的创建工作。 举个例子:平常我们在servlet或者service里面创建对象，都是使用new 的方式来直接创建对象，现在有了spring之后，我们就再也不new对象了，而是把对象创建的工作交给spring容器去维护。我们只需要告诉spring容器我们需要什么对象即可</p>
<p>IOC的作用：削减计算机程序的耦合(解除我们代码中的依赖关系)。</p>
<p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_004.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="第三节-IOC容器在Spring中的实现"><a href="#第三节-IOC容器在Spring中的实现" class="headerlink" title="第三节 IOC容器在Spring中的实现"></a>第三节 IOC容器在Spring中的实现</h3><p>Spring 的 IOC 容器就是 IOC 思想的一个落地的产品实现。IOC 容器中管理的组件也叫做 bean。在创建 bean 之前，首先需要创建 IOC 容器。Spring 提供了 IOC 容器的两种实现方式:</p>
<h4 id="1-BeanFactory"><a href="#1-BeanFactory" class="headerlink" title="1. BeanFactory"></a>1. BeanFactory</h4><p>这是 IOC 容器的基本实现，是 Spring 内部使用的接口。面向 Spring 框架本身，供Spring框架内部功能使用，不建议开发人员使用。</p>
<h4 id="2-ApplicationContext"><a href="#2-ApplicationContext" class="headerlink" title="2. ApplicationContext"></a>2. ApplicationContext</h4><p>BeanFactory 的子接口，提供了更多高级特性。面向 Spring 框架的使用者，几乎所有场合都使用 ApplicationContext 而不是底层的 BeanFactory。 </p>
<blockquote>
<p>以后在 Spring 环境下看到一个类或接口的名称中包含 ApplicationContext，那基本就可以断定，这个类或接口与 IOC 容器有关。</p>
</blockquote>
<h4 id="3-ApplicationContext的主要实现类"><a href="#3-ApplicationContext的主要实现类" class="headerlink" title="3. ApplicationContext的主要实现类"></a>3. ApplicationContext的主要实现类</h4><table>
<thead>
<tr>
<th>类型名</th>
<th>简介</th>
</tr>
</thead>
<tbody><tr>
<td><strong>ClassPathXmlApplicationContext</strong></td>
<td>通过读取类路径下的 XML 格式的配置文件创建 IOC 容器对象</td>
</tr>
<tr>
<td>FileSystemXmlApplicationContext</td>
<td>通过文件系统路径读取 XML 格式的配置文件创建 IOC 容器对象</td>
</tr>
<tr>
<td>ConfigurableApplicationContext</td>
<td>ApplicationContext 的子接口，包含一些扩展方法 refresh() 和 close() ，让 ApplicationContext 具有启动、关闭和刷新上下文的能力。</td>
</tr>
<tr>
<td><strong>AnnotationConfigApplicationContext</strong></td>
<td>可以实现基于Java的配置类加载Spring的应用上下文，创建IOC容器对象</td>
</tr>
<tr>
<td><strong>WebApplicationContext</strong></td>
<td>专门为 Web 应用准备，基于 Web 环境创建 IOC 容器对象，并将对象引用存入 ServletContext 域中。</td>
</tr>
</tbody></table>
<h2 id="第三章-Spring-IOC"><a href="#第三章-Spring-IOC" class="headerlink" title="第三章 Spring IOC"></a>第三章 Spring IOC</h2><h3 id="第一节-快速入门"><a href="#第一节-快速入门" class="headerlink" title="第一节 快速入门"></a>第一节 快速入门</h3><h4 id="1-目标"><a href="#1-目标" class="headerlink" title="1. 目标"></a>1. 目标</h4><p>1.1 让Spring IOC容器创建类的对象</p>
<p>1.2 从Spring IOC容器中获取对象</p>
<h4 id="2-思路"><a href="#2-思路" class="headerlink" title="2. 思路"></a>2. 思路</h4><p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_006.png" srcset="/img/loading.gif" lazyload></p>
<h4 id="3-具体实现"><a href="#3-具体实现" class="headerlink" title="3. 具体实现"></a>3. 具体实现</h4><h5 id="3-1-Maven依赖"><a href="#3-1-Maven依赖" class="headerlink" title="3.1 Maven依赖"></a>3.1 Maven依赖</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 基于Maven依赖传递性，导入spring-context依赖即可导入当前所需所有jar包 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-context<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- junit测试 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>4.12<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="3-2-创建类"><a href="#3-2-创建类" class="headerlink" title="3.2 创建类"></a>3.2 创建类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br>    <br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doWork</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;component do work ...&quot;</span>);<br>    &#125;<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-3-创建Spring-配置文件并且配置组件"><a href="#3-3-创建Spring-配置文件并且配置组件" class="headerlink" title="3.3 创建Spring 配置文件并且配置组件"></a>3.3 创建Spring 配置文件并且配置组件</h5><p>配置文件的存放路径建议放在resources根路径下，配置文件名字随意</p>
<p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_007.png" srcset="/img/loading.gif" lazyload></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans</span></span><br><span class="hljs-string"><span class="hljs-tag">       http://www.springframework.org/schema/beans/spring-beans.xsd&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">        每一个实现类就对应一个bean标签</span><br><span class="hljs-comment">            id属性: 对象的唯一标识，根据这个唯一标识，就可以从核心容器中获取对象</span><br><span class="hljs-comment">            class属性: 对象所属的实现类的全限定名</span><br><span class="hljs-comment">    --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component.HappyComponent&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>bean标签：通过配置bean标签告诉IOC容器需要创建对象的组件是什么</li>
<li>id属性：bean的唯一标识</li>
<li>class属性：组件类的全类名</li>
</ul>
<h5 id="3-4-从核心容器中获取对象"><a href="#3-4-从核心容器中获取对象" class="headerlink" title="3.4 从核心容器中获取对象"></a>3.4 从核心容器中获取对象</h5><h6 id="方式一：根据id获取"><a href="#方式一：根据id获取" class="headerlink" title="方式一：根据id获取"></a>方式一：根据id获取</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IOCTest</span> </span>&#123;<br>    <br>    <span class="hljs-comment">// 创建 IOC 容器对象，为便于其他实验方法使用声明为成员变量</span><br>    <span class="hljs-keyword">private</span> ApplicationContext iocContainer = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">&quot;applicationContext.xml&quot;</span>);<br>    <br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testExperiment01</span><span class="hljs-params">()</span> </span>&#123;<br>    <br>        <span class="hljs-comment">// 从 IOC 容器对象中获取bean，也就是组件对象</span><br>        HappyComponent happyComponent = (HappyComponent) iocContainer.getBean(<span class="hljs-string">&quot;happyComponent&quot;</span>);<br>        happyComponent.doWork();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="方式二：根据类型获取"><a href="#方式二：根据类型获取" class="headerlink" title="方式二：根据类型获取"></a>方式二：根据类型获取</h6><p>如果该类型在核心容器中只有一个对象：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testExperiment02</span><span class="hljs-params">()</span> </span>&#123;<br>    <br>    HappyComponent component = iocContainer.getBean(HappyComponent.class);<br>    <br>    component.doWork();<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<p>如果该类型在核心容器中有多个对象：那么根据类型获取时会抛出异常，具体异常信息如下</p>
<blockquote>
<p>org.springframework.beans.factory.<strong>NoUniqueBeanDefinitionException</strong>: No qualifying bean of type ‘com.atguigu.ioc.component.HappyComponent’ available: expected single matching bean but found 2: happyComponent,happyComponent2</p>
</blockquote>
<h6 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h6><p>如果组件类实现了接口，根据接口类型可以获取 bean对象 吗？</p>
<blockquote>
<p>可以，前提是bean对象唯一</p>
</blockquote>
<p>如果一个接口有多个实现类，这些实现类都配置了 bean，根据接口类型可以获取 bean 吗？</p>
<blockquote>
<p>不行，因为bean对象不唯一</p>
</blockquote>
<h6 id="结论"><a href="#结论" class="headerlink" title="结论"></a>结论</h6><p>根据类型来获取bean时，在满足bean唯一性的前提下，其实只是看：『对象 <strong>instanceof</strong> 指定的类型』的返回结果，只要返回的是true就可以认定为和类型匹配，能够获取到。 </p>
<h3 id="第二节-依赖注入"><a href="#第二节-依赖注入" class="headerlink" title="第二节 依赖注入"></a>第二节 依赖注入</h3><p>依赖注入全称是 dependency Injection 翻译过来是依赖注入.其实就是如果spring核心容器管理的某一个类中存在属性，需要spring核心容器在创建该类实例的时候，顺便给这个对象里面的属性进行赋值。</p>
<h4 id="1-setter方法注入"><a href="#1-setter方法注入" class="headerlink" title="1. setter方法注入"></a>1. setter方法注入</h4><p>如果某个Bean对象的属性有对应的setter方法，那我们可以在配置文件中使用setter方法对属性进行依赖注入</p>
<h5 id="1-1-注入简单类型数据"><a href="#1-1-注入简单类型数据" class="headerlink" title="1.1 注入简单类型数据"></a>1.1 注入简单类型数据</h5><h6 id="1-1-1-给组件类添加一个简单类型属性"><a href="#1-1-1-给组件类添加一个简单类型属性" class="headerlink" title="1.1.1 给组件类添加一个简单类型属性"></a>1.1.1 给组件类添加一个简单类型属性</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.component</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:10</span><br><span class="hljs-comment"> * 给一个对象的成员变量赋值的方式:</span><br><span class="hljs-comment"> * 1. 调用set方法</span><br><span class="hljs-comment"> * 2. 通过构造器</span><br><span class="hljs-comment"> * 3. 通过暴力反射</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String username;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getUsername</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUsername</span><span class="hljs-params">(String username)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.username = username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doWork</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;component do work ...&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-1-2-在配置时给属性指定值"><a href="#1-1-2-在配置时给属性指定值" class="headerlink" title="1.1.2 在配置时给属性指定值"></a>1.1.2 在配置时给属性指定值</h6><p>通过property标签配置的属性值会通过setXxx()方法注入，大家可以通过debug方式验证一下 </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">        依赖注入:给核心容器中的Bean对象的成员变量赋值</span><br><span class="hljs-comment">        setter方法进行依赖注入:</span><br><span class="hljs-comment">            在要进行依赖注入的bean标签中添加&lt;property&gt;子标签,该子标签的name属性就是要赋值的成员变量名</span><br><span class="hljs-comment">            前提是这个属性一定要有set方法,name属性的值应该是&quot;setXXX&quot;后面的&quot;XXX&quot;首字母改小写</span><br><span class="hljs-comment">            1. 注入简单类型数据: 那么我们使用property标签的value属性给简单类型的成员变量赋值</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"></span><br><span class="hljs-comment">            2. 注入Bean类型数据: 那么我们使用property标签的ref属性给Bean类型的成员变量赋值，</span><br><span class="hljs-comment">                               ref属性的值就是要赋值的Bean类型的对象在核心容器中的id</span><br><span class="hljs-comment">    --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;奥巴马&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-1-3-测试代码"><a href="#1-1-3-测试代码" class="headerlink" title="1.1.3 测试代码"></a>1.1.3 测试代码</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testGetHappyComponent</span><span class="hljs-params">()</span></span>&#123;<br>    <span class="hljs-comment">//2. 使用核心容器对象获取HappyComponent对象</span><br>    <span class="hljs-comment">//根据id获取:获取ioc容器中id为&quot;happyComponent&quot;的对象</span><br>    HappyComponent happyComponent1 = (HappyComponent) act.getBean(<span class="hljs-string">&quot;happyComponent&quot;</span>);<br>    <br>    <span class="hljs-comment">//3. 使用HappyComponent对象获取username属性</span><br>    System.out.println(happyComponent1.getUsername());<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-2-注入Bean类型数据"><a href="#1-2-注入Bean类型数据" class="headerlink" title="1.2 注入Bean类型数据"></a>1.2 注入Bean类型数据</h5><h6 id="1-2-1-声明新的组件类UserServlet"><a href="#1-2-1-声明新的组件类UserServlet" class="headerlink" title="1.2.1 声明新的组件类UserServlet"></a>1.2.1 声明新的组件类UserServlet</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.servlet;<br><br><span class="hljs-keyword">import</span> com.atguigu.service.UserService;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.servlet</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:28</span><br><span class="hljs-comment"> * 1. IOC : 由核心容器创建Bean对象</span><br><span class="hljs-comment"> * 2. DI(依赖注入) : 给核心容器中的Bean对象的成员变量赋值</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserServlet</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> UserService userService;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserService</span><span class="hljs-params">(UserService userService)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.userService = userService;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span></span>&#123;<br>        userService.sayHello();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-2-2-声明新的组件接口UserService和实现类UserServiceImpl"><a href="#1-2-2-声明新的组件接口UserService和实现类UserServiceImpl" class="headerlink" title="1.2.2 声明新的组件接口UserService和实现类UserServiceImpl"></a>1.2.2 声明新的组件接口UserService和实现类UserServiceImpl</h6><p>UserService接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.service;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.service</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:27</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserService</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span></span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>UserServiceImpl实现类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.service.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.component.HappyComponent;<br><span class="hljs-keyword">import</span> com.atguigu.service.UserService;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.service</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:33</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserService</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> HappyComponent happyComponent;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setHappyComponent</span><span class="hljs-params">(HappyComponent happyComponent)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.happyComponent = happyComponent;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;hello,&quot;</span>+happyComponent.getUsername());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-2-3-在UserService对象中注入HappyComponent对象"><a href="#1-2-3-在UserService对象中注入HappyComponent对象" class="headerlink" title="1.2.3 在UserService对象中注入HappyComponent对象"></a>1.2.3 在UserService对象中注入HappyComponent对象</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">        使用依赖注入给HappyComponent属性赋值</span><br><span class="hljs-comment">    --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.service.impl.UserServiceImpl&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;happyComponent&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-2-4-在UserServlet对象中注入UserService对象"><a href="#1-2-4-在UserServlet对象中注入UserService对象" class="headerlink" title="1.2.4 在UserServlet对象中注入UserService对象"></a>1.2.4 在UserServlet对象中注入UserService对象</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">        你想让ioc容器创建什么对象，就将那个类配置到bean标签中</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">        使用依赖注入给UserService属性赋值</span><br><span class="hljs-comment">    --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userServlet&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.servlet.UserServlet&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;userService&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>这个操作在 IDEA 中有提示： </p>
<p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_009.png" srcset="/img/loading.gif" lazyload></p>
<h6 id="1-2-5-测试"><a href="#1-2-5-测试" class="headerlink" title="1.2.5 测试"></a>1.2.5 测试</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSayHello</span><span class="hljs-params">()</span></span>&#123;<br>    <span class="hljs-comment">//通过ioc容器获取UserServlet的对象</span><br>    UserServlet userServlet = (UserServlet) act.getBean(<span class="hljs-string">&quot;userServlet&quot;</span>);<br>    <span class="hljs-comment">//调用UserServlet的sayHello()方法</span><br>    userServlet.sayHello();<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-2-6-易错点"><a href="#1-2-6-易错点" class="headerlink" title="1.2.6 易错点"></a>1.2.6 易错点</h6><blockquote>
<p>如果错把ref属性写成了value属性，会抛出异常： Caused by: java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String’ to required type ‘com.atguigu.ioc.component.HappyMachine’ for property ‘happyMachine’: no matching editors or conversion strategy found 意思是不能把String类型转换成我们要的HappyMachine类型 说明我们使用value属性时，Spring只把这个属性看做一个普通的字符串，不会认为这是一个bean的id，更不会根据它去找到bean来赋值</p>
</blockquote>
<h5 id="1-3-注入内部Bean类型数据-了解"><a href="#1-3-注入内部Bean类型数据-了解" class="headerlink" title="1.3 注入内部Bean类型数据(了解)"></a>1.3 注入内部Bean类型数据(了解)</h5><h6 id="1-3-1-重新配置原组件"><a href="#1-3-1-重新配置原组件" class="headerlink" title="1.3.1 重新配置原组件"></a>1.3.1 重新配置原组件</h6><p>在bean里面配置的bean就是内部bean，内部bean只能在当前bean内部使用，在其他地方不能使用。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.service.impl.UserServiceImpl&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;happyComponent&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--这个bean标签创建的HappyComponent对象只能用于给UserServiceImpl的happyComponent属性赋值,别的地方不能使用--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;aobama&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-3-2-测试"><a href="#1-3-2-测试" class="headerlink" title="1.3.2 测试"></a>1.3.2 测试</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testExperiment04</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-comment">//通过核心容器获取UserService的对象:我为什么要使用接口类型接收实现类的对象,为了解耦</span><br>    UserService userService = (UserService) act.getBean(<span class="hljs-string">&quot;userService&quot;</span>);<br>    userService.sayHello();<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-4-引入外部属性文件用于给Bean注入属性"><a href="#1-4-引入外部属性文件用于给Bean注入属性" class="headerlink" title="1.4 引入外部属性文件用于给Bean注入属性"></a>1.4 引入外部属性文件用于给Bean注入属性</h5><h6 id="1-4-1-添加Maven依赖"><a href="#1-4-1-添加Maven依赖" class="headerlink" title="1.4.1 添加Maven依赖"></a>1.4.1 添加Maven依赖</h6><p>这个依赖只是为了使用Druid连接池，而不是引入外部属性文件所必须的</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- MySQL驱动 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.1.3<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-comment">&lt;!-- 数据源 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>druid<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0.31<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-4-2-创建外部属性文件"><a href="#1-4-2-创建外部属性文件" class="headerlink" title="1.4.2 创建外部属性文件"></a>1.4.2 创建外部属性文件</h6><p><img src="http://www.slx.blue/2021/12/04/spring-day01/tu_010.png" srcset="/img/loading.gif" lazyload></p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-meta">jdbc.user</span>=<span class="hljs-string">root</span><br><span class="hljs-meta">jdbc.password</span>=<span class="hljs-string">123456</span><br><span class="hljs-meta">jdbc.url</span>=<span class="hljs-string">jdbc:mysql://localhost:3306/mybatis-example</span><br><span class="hljs-meta">jdbc.driver</span>=<span class="hljs-string">com.mysql.jdbc.Driver</span><br></code></pre></td></tr></table></figure>

<h6 id="1-4-3-在spring的配置文件中引入jdbc-properties文件"><a href="#1-4-3-在spring的配置文件中引入jdbc-properties文件" class="headerlink" title="1.4.3 在spring的配置文件中引入jdbc.properties文件"></a>1.4.3 在spring的配置文件中引入jdbc.properties文件</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 引入外部属性文件 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:property-placeholder</span> <span class="hljs-attr">location</span>=<span class="hljs-string">&quot;classpath:jdbc.properties&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-4-4-在spring的配置文件中使用引入的jdbc-properties文件中的数据"><a href="#1-4-4-在spring的配置文件中使用引入的jdbc-properties文件中的数据" class="headerlink" title="1.4.4 在spring的配置文件中使用引入的jdbc.properties文件中的数据"></a>1.4.4 在spring的配置文件中使用引入的jdbc.properties文件中的数据</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--[重要]给bean的属性赋值：引入外部属性文件 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;druidDataSource&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.alibaba.druid.pool.DruidDataSource&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;url&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.url&#125;&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;driverClassName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.driver&#125;&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.user&#125;&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-4-5-测试"><a href="#1-4-5-测试" class="headerlink" title="1.4.5 测试"></a>1.4.5 测试</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testExperiment06</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> SQLException </span>&#123;<br>    DataSource dataSource = iocContainer.getBean(DataSource.class);<br>    Connection connection = dataSource.getConnection();<br>    System.out.println(<span class="hljs-string">&quot;connection = &quot;</span> + connection);<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-4-6-结论"><a href="#1-4-6-结论" class="headerlink" title="1.4.6 结论"></a>1.4.6 结论</h6><p>标签的value属性: 注入简单类型数据</p>
<p>标签的ref属性:用于引入IOC容器中的Bean对象的id，注入Bean对象类型的数据</p>
<h5 id="1-5-注入集合类型属性-了解"><a href="#1-5-注入集合类型属性-了解" class="headerlink" title="1.5 注入集合类型属性(了解)"></a>1.5 注入集合类型属性(了解)</h5><h6 id="1-5-1-给组件类添加集合类型属性"><a href="#1-5-1-给组件类添加集合类型属性" class="headerlink" title="1.5.1 给组件类添加集合类型属性"></a>1.5.1 给组件类添加集合类型属性</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.component</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:10</span><br><span class="hljs-comment"> * 给一个对象的成员变量赋值的方式:</span><br><span class="hljs-comment"> * 1. 调用set方法</span><br><span class="hljs-comment"> * 2. 通过构造器</span><br><span class="hljs-comment"> * 3. 通过暴力反射</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-keyword">private</span> List&lt;String&gt; memberList;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;String&gt; <span class="hljs-title">getMemberList</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> memberList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setMemberList</span><span class="hljs-params">(List&lt;String&gt; memberList)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.memberList = memberList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getUsername</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUsername</span><span class="hljs-params">(String username)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.username = username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doWork</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;component do work ...&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-5-2-配置"><a href="#1-5-2-配置" class="headerlink" title="1.5.2 配置"></a>1.5.2 配置</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;奥巴马&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--注入集合类型的数据--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;memberList&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--&lt;list&gt;</span><br><span class="hljs-comment">                &lt;value&gt;张三&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;李四&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;王五&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;赵六&lt;/value&gt;</span><br><span class="hljs-comment">            &lt;/list&gt;--&gt;</span><br><br>        <span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">                使用set标签注入集合可以去重</span><br><span class="hljs-comment">            --&gt;</span><br>        <span class="hljs-comment">&lt;!--&lt;set&gt;</span><br><span class="hljs-comment">                &lt;value&gt;张三&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;李四&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;王五&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;赵六&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;王五&lt;/value&gt;</span><br><span class="hljs-comment">            &lt;/set&gt;--&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">array</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>张三<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>李四<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>王五<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>赵六<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>王五<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-6-注入Map类型属性-了解"><a href="#1-6-注入Map类型属性-了解" class="headerlink" title="1.6 注入Map类型属性(了解)"></a>1.6 注入Map类型属性(了解)</h5><h6 id="1-6-1-给组件类添加Map类型属性"><a href="#1-6-1-给组件类添加Map类型属性" class="headerlink" title="1.6.1 给组件类添加Map类型属性"></a>1.6.1 给组件类添加Map类型属性</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.component</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:10</span><br><span class="hljs-comment"> * 给一个对象的成员变量赋值的方式:</span><br><span class="hljs-comment"> * 1. 调用set方法</span><br><span class="hljs-comment"> * 2. 通过构造器</span><br><span class="hljs-comment"> * 3. 通过暴力反射</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-keyword">private</span> List&lt;String&gt; memberList;<br>    <span class="hljs-keyword">private</span> Map&lt;String,String&gt; managerMap;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Map&lt;String, String&gt; <span class="hljs-title">getManagerMap</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> managerMap;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setManagerMap</span><span class="hljs-params">(Map&lt;String, String&gt; managerMap)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.managerMap = managerMap;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;String&gt; <span class="hljs-title">getMemberList</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> memberList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setMemberList</span><span class="hljs-params">(List&lt;String&gt; memberList)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.memberList = memberList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getUsername</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUsername</span><span class="hljs-params">(String username)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.username = username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doWork</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;component do work ...&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-6-2-配置"><a href="#1-6-2-配置" class="headerlink" title="1.6.2 配置"></a>1.6.2 配置</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--注入Map类型的数据--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;managerMap&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--&lt;map&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k1&quot; value=&quot;v1&quot; &gt;&lt;/entry&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k2&quot; value=&quot;v2&quot;&gt;&lt;/entry&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k3&quot; value=&quot;v3&quot;&gt;&lt;/entry&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k4&quot; value=&quot;v4&quot;&gt;&lt;/entry&gt;</span><br><span class="hljs-comment">            &lt;/map&gt;--&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">props</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k1&quot;</span>&gt;</span>v1<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k2&quot;</span>&gt;</span>v2<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k3&quot;</span>&gt;</span>v3<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k4&quot;</span>&gt;</span>v4<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">props</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-7-注入Bean的集合类型-了解"><a href="#1-7-注入Bean的集合类型-了解" class="headerlink" title="1.7 注入Bean的集合类型(了解)"></a>1.7 注入Bean的集合类型(了解)</h5><h6 id="1-7-1-给组件添加Bean的集合类型属性"><a href="#1-7-1-给组件添加Bean的集合类型属性" class="headerlink" title="1.7.1 给组件添加Bean的集合类型属性"></a>1.7.1 给组件添加Bean的集合类型属性</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><span class="hljs-keyword">import</span> java.util.Map;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.component</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  10:10</span><br><span class="hljs-comment"> * 给一个对象的成员变量赋值的方式:</span><br><span class="hljs-comment"> * 1. 调用set方法</span><br><span class="hljs-comment"> * 2. 通过构造器</span><br><span class="hljs-comment"> * 3. 通过暴力反射</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-keyword">private</span> List&lt;String&gt; memberList;<br>    <span class="hljs-keyword">private</span> Map&lt;String,String&gt; managerMap;<br>    <span class="hljs-keyword">private</span> List&lt;User&gt; userList;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;User&gt; <span class="hljs-title">getUserList</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> userList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserList</span><span class="hljs-params">(List&lt;User&gt; userList)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.userList = userList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Map&lt;String, String&gt; <span class="hljs-title">getManagerMap</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> managerMap;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setManagerMap</span><span class="hljs-params">(Map&lt;String, String&gt; managerMap)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.managerMap = managerMap;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;String&gt; <span class="hljs-title">getMemberList</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> memberList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setMemberList</span><span class="hljs-params">(List&lt;String&gt; memberList)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.memberList = memberList;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getUsername</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUsername</span><span class="hljs-params">(String username)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.username = username;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doWork</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;component do work ...&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>User类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.component</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  14:57</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String name;<br>    <span class="hljs-keyword">private</span> String address;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;User&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, address=&#x27;&quot;</span> + address + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> name;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.name = name;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getAddress</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> address;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span><span class="hljs-params">(String address)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.address = address;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-7-2-配置"><a href="#1-7-2-配置" class="headerlink" title="1.7.2 配置"></a>1.7.2 配置</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;奥巴马&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--注入集合类型的数据--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;memberList&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--&lt;list&gt;</span><br><span class="hljs-comment">                &lt;value&gt;张三&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;李四&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;王五&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;赵六&lt;/value&gt;</span><br><span class="hljs-comment">            &lt;/list&gt;--&gt;</span><br><br>        <span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">                使用set标签注入集合可以去重</span><br><span class="hljs-comment">            --&gt;</span><br>        <span class="hljs-comment">&lt;!--&lt;set&gt;</span><br><span class="hljs-comment">                &lt;value&gt;张三&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;李四&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;王五&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;赵六&lt;/value&gt;</span><br><span class="hljs-comment">                &lt;value&gt;王五&lt;/value&gt;</span><br><span class="hljs-comment">            &lt;/set&gt;--&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">array</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>张三<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>李四<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>王五<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>赵六<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>王五<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">array</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!--注入Map类型的数据--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;managerMap&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--&lt;map&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k1&quot; value=&quot;v1&quot; &gt;&lt;/entry&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k2&quot; value=&quot;v2&quot;&gt;&lt;/entry&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k3&quot; value=&quot;v3&quot;&gt;&lt;/entry&gt;</span><br><span class="hljs-comment">                &lt;entry key=&quot;k4&quot; value=&quot;v4&quot;&gt;&lt;/entry&gt;</span><br><span class="hljs-comment">            &lt;/map&gt;--&gt;</span><br><br>        <span class="hljs-tag">&lt;<span class="hljs-name">props</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k1&quot;</span>&gt;</span>v1<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k2&quot;</span>&gt;</span>v2<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k3&quot;</span>&gt;</span>v3<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">prop</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;k4&quot;</span>&gt;</span>v4<span class="hljs-tag">&lt;/<span class="hljs-name">prop</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">props</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!--注入Bean的集合类型--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;userList&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">list</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.User&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;name&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;张三&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;address&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;深圳&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.User&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;name&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;李四&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;address&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;广州&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.User&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;name&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;王五&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;address&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;北京&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">list</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="2-构造器注入-了解"><a href="#2-构造器注入-了解" class="headerlink" title="2. 构造器注入(了解)"></a>2. 构造器注入(了解)</h4><p>在前面我们通过<code>&lt;bean&gt;</code>标签配置Bean对象，其实是执行Bean类的无参构造函数创建的对象，当Bean类包含有参构造函数的时候，我们在配置文件中可以通过有参构造函数进行配置注入</p>
<h5 id="2-1-声明组件类"><a href="#2-1-声明组件类" class="headerlink" title="2.1 声明组件类"></a>2.1 声明组件类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br>    <br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyTeam</span> </span>&#123;<br>        <br>    <span class="hljs-keyword">private</span> String teamName;<br>    <span class="hljs-keyword">private</span> Integer memberCount;<br>    <span class="hljs-keyword">private</span> Double memberSalary;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getTeamName</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> teamName;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setTeamName</span><span class="hljs-params">(String teamName)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.teamName = teamName;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Integer <span class="hljs-title">getMemberCount</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> memberCount;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setMemberCount</span><span class="hljs-params">(Integer memberCount)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.memberCount = memberCount;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Double <span class="hljs-title">getMemberSalary</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> memberSalary;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setMemberSalary</span><span class="hljs-params">(Double memberSalary)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.memberSalary = memberSalary;<br>    &#125;<br>    <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;HappyTeam&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;teamName=&#x27;&quot;</span> + teamName + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, memberCount=&quot;</span> + memberCount +<br>                <span class="hljs-string">&quot;, memberSalary=&quot;</span> + memberSalary +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">HappyTeam</span><span class="hljs-params">(String teamName, Integer memberCount, Double memberSalary)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.teamName = teamName;<br>        <span class="hljs-keyword">this</span>.memberCount = memberCount;<br>        <span class="hljs-keyword">this</span>.memberSalary = memberSalary;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">HappyTeam</span><span class="hljs-params">()</span> </span>&#123;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="2-2-配置构造器注入"><a href="#2-2-配置构造器注入" class="headerlink" title="2.2 配置构造器注入"></a>2.2 配置构造器注入</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 给bean的属性赋值：构造器注入 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyTeam&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component.HappyTeam&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;happyCorps&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;10&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;1000.55&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-3-测试"><a href="#2-3-测试" class="headerlink" title="2.3 测试"></a>2.3 测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testExperiment08</span><span class="hljs-params">()</span> </span>&#123;<br>    <br>    HappyTeam happyTeam = iocContainer.getBean(HappyTeam.class);<br>    <br>    System.out.println(<span class="hljs-string">&quot;happyTeam = &quot;</span> + happyTeam);<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="2-4-补充"><a href="#2-4-补充" class="headerlink" title="2.4 补充"></a>2.4 补充</h5><p>constructor-arg标签还有两个属性可以进一步描述构造器参数：</p>
<ul>
<li>index属性：指定参数所在位置的索引（从0开始）</li>
<li>name属性：指定参数名</li>
</ul>
<h4 id="3-特殊值处理-了解"><a href="#3-特殊值处理-了解" class="headerlink" title="3. 特殊值处理(了解)"></a>3. 特殊值处理(了解)</h4><h5 id="3-1-声明一个类用于测试"><a href="#3-1-声明一个类用于测试" class="headerlink" title="3.1 声明一个类用于测试"></a>3.1 声明一个类用于测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br>    <br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PropValue</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String commonValue;<br>    <span class="hljs-keyword">private</span> String expression;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getCommonValue</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> commonValue;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setCommonValue</span><span class="hljs-params">(String commonValue)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.commonValue = commonValue;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getExpression</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> expression;<br>    &#125;<br>    <br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setExpression</span><span class="hljs-params">(String expression)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.expression = expression;<br>    &#125;<br>    <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;PropValue&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;commonValue=&#x27;&quot;</span> + commonValue + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&quot;, expression=&#x27;&quot;</span> + expression + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">PropValue</span><span class="hljs-params">(String commonValue, String expression)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.commonValue = commonValue;<br>        <span class="hljs-keyword">this</span>.expression = expression;<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">PropValue</span><span class="hljs-params">()</span> </span>&#123;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-2-null值"><a href="#3-2-null值" class="headerlink" title="3.2 null值"></a>3.2 null值</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;commonValue&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- null标签：将一个属性值明确设置为null --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">null</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="3-3-当value值中有特殊字符时"><a href="#3-3-当value值中有特殊字符时" class="headerlink" title="3.3 当value值中有特殊字符时"></a>3.3 当value值中有特殊字符时</h5><h6 id="3-3-1-使用XML实体字符-转义符-解决"><a href="#3-3-1-使用XML实体字符-转义符-解决" class="headerlink" title="3.3.1 使用XML实体字符(转义符)解决"></a>3.3.1 使用XML实体字符(转义符)解决</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;propValue&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component.PropValue&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 小于号在XML文档中用来定义标签的开始，不能随便使用 --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 解决方案一：使用XML实体来代替 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;expression&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;a <span class="hljs-symbol">&amp;lt;</span> b&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="3-3-2-使用CDATA解决"><a href="#3-3-2-使用CDATA解决" class="headerlink" title="3.3.2 使用CDATA解决"></a>3.3.2 使用CDATA解决</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;propValue&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component.PropValue&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;expression&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 解决方案二：使用CDATA节 --&gt;</span><br>        <span class="hljs-comment">&lt;!-- CDATA中的C代表Character，是文本、字符的含义，CDATA就表示纯文本数据 --&gt;</span><br>        <span class="hljs-comment">&lt;!-- XML解析器看到CDATA节就知道这里是纯文本，就不会当作XML标签或属性来解析 --&gt;</span><br>        <span class="hljs-comment">&lt;!-- 所以CDATA节中写什么符号都随意 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>&lt;![CDATA[a &lt; b]]&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="4-p命名空间方式注入-了解"><a href="#4-p命名空间方式注入-了解" class="headerlink" title="4. p命名空间方式注入(了解)"></a>4. p命名空间方式注入(了解)</h4><h5 id="4-1-引入p命名空间的约束"><a href="#4-1-引入p命名空间的约束" class="headerlink" title="4.1 引入p命名空间的约束"></a>4.1 引入p命名空间的约束</h5><p>使用 p 名称空间需要导入相关的 XML 约束，在 IDEA 的协助下导入即可： </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/context&quot;</span> <span class="hljs-attr">xmlns:p</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/p&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<h5 id="4-2-使用p命名空间注入"><a href="#4-2-使用p命名空间注入" class="headerlink" title="4.2 使用p命名空间注入"></a>4.2 使用p命名空间注入</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--注入简单类型数据--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span> <span class="hljs-attr">p:username</span>=<span class="hljs-string">&quot;奥拉夫&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>    <br><br><span class="hljs-comment">&lt;!--注入Bean类型数据--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userServlet&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.servlet.UserServlet&quot;</span> <span class="hljs-attr">p:userService-ref</span>=<span class="hljs-string">&quot;userService&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.service.impl.UserServiceImpl&quot;</span> <span class="hljs-attr">p:happyComponent-ref</span>=<span class="hljs-string">&quot;happyComponent&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="4-3-测试"><a href="#4-3-测试" class="headerlink" title="4.3 测试"></a>4.3 测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSayHello</span><span class="hljs-params">()</span></span>&#123;<br>    <span class="hljs-comment">//通过ioc容器获取UserServlet的对象</span><br>    UserServlet userServlet = (UserServlet) act.getBean(<span class="hljs-string">&quot;userServlet&quot;</span>);<br>    <span class="hljs-comment">//调用UserServlet的sayHello()方法</span><br>    userServlet.sayHello();<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="5-自动装配"><a href="#5-自动装配" class="headerlink" title="5. 自动装配"></a>5. 自动装配</h4><p>所谓自动装配就是一个组件需要其他组件时，由 IOC 容器负责找到那个需要的组件，并装配进去。  </p>
<h5 id="5-1-配置"><a href="#5-1-配置" class="headerlink" title="5.1 配置"></a>5.1 配置</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent2&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span> <span class="hljs-attr">p:username</span>=<span class="hljs-string">&quot;奥拉夫&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span> <span class="hljs-attr">p:username</span>=<span class="hljs-string">&quot;奥巴马&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>    <br><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">        你想让ioc容器创建什么对象，就将那个类配置到bean标签中</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">        使用依赖注入给UserService属性赋值</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">        自动装配:autowire属性表示自动装配，就是不需要你去管依赖注入，IOC容器会自动进行依赖注入。它的取值有如下两个</span><br><span class="hljs-comment">            1. byName:根据要注入的属性名和Bean对象的id的对应关系去注入</span><br><span class="hljs-comment">            2. byType:表示核心容器会自动在自身容器中查找一个该类型的对象，给成员变量赋值</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"></span><br><span class="hljs-comment">    --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userServlet&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.servlet.UserServlet&quot;</span> <span class="hljs-attr">autowire</span>=<span class="hljs-string">&quot;byType&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;userService&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.service.impl.UserServiceImpl&quot;</span> <span class="hljs-attr">autowire</span>=<span class="hljs-string">&quot;byName&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>    <br></code></pre></td></tr></table></figure>

<h5 id="5-2-测试"><a href="#5-2-测试" class="headerlink" title="5.2 测试"></a>5.2 测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSayHello</span><span class="hljs-params">()</span></span>&#123;<br>    <span class="hljs-comment">//通过ioc容器获取UserServlet的对象</span><br>    UserServlet userServlet = (UserServlet) act.getBean(<span class="hljs-string">&quot;userServlet&quot;</span>);<br>    <span class="hljs-comment">//调用UserServlet的sayHello()方法</span><br>    userServlet.sayHello();<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第三节-Bean的作用域和生命周期"><a href="#第三节-Bean的作用域和生命周期" class="headerlink" title="第三节 Bean的作用域和生命周期"></a>第三节 Bean的作用域和生命周期</h3><h4 id="1-Bean的作用域"><a href="#1-Bean的作用域" class="headerlink" title="1. Bean的作用域"></a>1. Bean的作用域</h4><h5 id="1-1-概念"><a href="#1-1-概念" class="headerlink" title="1.1 概念"></a>1.1 概念</h5><p>在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围，各取值含义参加下表：</p>
<table>
<thead>
<tr>
<th>取值</th>
<th>含义</th>
<th>创建对象的时机</th>
</tr>
</thead>
<tbody><tr>
<td>singleton</td>
<td>在IOC容器中，这个bean的对象始终为单实例</td>
<td>IOC容器初始化时</td>
</tr>
<tr>
<td>prototype</td>
<td>这个bean在IOC容器中有多个实例</td>
<td>获取bean时</td>
</tr>
</tbody></table>
<p>如果是在WebApplicationContext环境下还会有另外两个作用域（但几乎不用）：</p>
<table>
<thead>
<tr>
<th>取值</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>request</td>
<td>在一个请求范围内有效</td>
</tr>
<tr>
<td>session</td>
<td>在一个会话范围内有效</td>
</tr>
</tbody></table>
<h5 id="1-2-配置"><a href="#1-2-配置" class="headerlink" title="1.2 配置"></a>1.2 配置</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- scope属性：取值singleton（默认值），bean在IOC容器中只有一个实例，IOC容器初始化时创建对象 --&gt;</span><br><span class="hljs-comment">&lt;!-- scope属性：取值prototype，bean在IOC容器中可以有多个实例，getBean()时创建对象 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComment&quot;</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;prototype&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComment&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-3-测试"><a href="#1-3-测试" class="headerlink" title="1.3 测试"></a>1.3 测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testGetBean</span><span class="hljs-params">()</span> </span>&#123;<br>    HappyComment happyComment01 = (HappyComment) act.getBean(<span class="hljs-string">&quot;happyComment&quot;</span>);<br>    HappyComment happyComment02 = (HappyComment) act.getBean(<span class="hljs-string">&quot;happyComment&quot;</span>);<br>    <br>    System.out.println(happyComment01 == happyComment02);<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="2-Bean的生命周期-了解"><a href="#2-Bean的生命周期-了解" class="headerlink" title="2. Bean的生命周期(了解)"></a>2. Bean的生命周期(了解)</h4><h5 id="2-1-bean的生命周期清单"><a href="#2-1-bean的生命周期清单" class="headerlink" title="2.1 bean的生命周期清单"></a>2.1 bean的生命周期清单</h5><ul>
<li>bean对象创建（调用无参构造器）</li>
<li>给bean对象设置属性（依赖注入）</li>
<li>bean对象初始化之前操作（由bean的后置处理器前置方法负责）</li>
<li>bean对象初始化（需在配置bean时指定初始化方法）</li>
<li>bean对象初始化之后操作（由bean的后置处理器后置方法负责）</li>
<li>bean对象就绪可以使用</li>
<li>bean对象销毁（需在配置bean时指定销毁方法）</li>
<li>IOC容器关闭</li>
</ul>
<h5 id="2-2-指定bean的初始化方法和销毁方法"><a href="#2-2-指定bean的初始化方法和销毁方法" class="headerlink" title="2.2 指定bean的初始化方法和销毁方法"></a>2.2 指定bean的初始化方法和销毁方法</h5><h6 id="2-2-1-创建两个方法作为初始化和销毁方法"><a href="#2-2-1-创建两个方法作为初始化和销毁方法" class="headerlink" title="2.2.1 创建两个方法作为初始化和销毁方法"></a>2.2.1 创建两个方法作为初始化和销毁方法</h6><p>用com.atguigu.component.HappyComponent类测试，在类中加俩方法： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:PACKAGE_NAME</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-29  16:02</span><br><span class="hljs-comment"> * 目标:让HappyComponent对象创建的时候，就执行initLifeCircle()方法，在HappyComponent对象销毁之前就执行destroyLifeCircle()</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initLifeCircle</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;HappyComponent对象创建了，我可以做一些初始化操作...&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroyLifeCircle</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;HappyComponent对象销毁了，我可以做一些数据备份工作...&quot;</span>);<br>    &#125;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sayHello</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;hello world&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-2-2-配置bean时指定初始化和销毁方法"><a href="#2-2-2-配置bean时指定初始化和销毁方法" class="headerlink" title="2.2.2 配置bean时指定初始化和销毁方法"></a>2.2.2 配置bean时指定初始化和销毁方法</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">        bean标签的scope属性表示这个Bean对象的范围:</span><br><span class="hljs-comment">            1. singleton(默认取值): 单例</span><br><span class="hljs-comment">            2. prototype: 多例</span><br><span class="hljs-comment">        bean标签的init-method属性是用于配置这个Bean对象的初始化方法,</span><br><span class="hljs-comment">        bean标签的destroy-method属性是用于配置这个Bean对象的销毁方法</span><br><span class="hljs-comment">    --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponent&quot;</span></span><br><span class="hljs-tag">      <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;prototype&quot;</span></span><br><span class="hljs-tag">      <span class="hljs-attr">init-method</span>=<span class="hljs-string">&quot;initLifeCircle&quot;</span></span><br><span class="hljs-tag">      <span class="hljs-attr">destroy-method</span>=<span class="hljs-string">&quot;destroyLifeCircle&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-3-bean的后置处理器"><a href="#2-3-bean的后置处理器" class="headerlink" title="2.3 bean的后置处理器"></a>2.3 bean的后置处理器</h5><h6 id="2-3-1-创建后置处理器类"><a href="#2-3-1-创建后置处理器类" class="headerlink" title="2.3.1 创建后置处理器类"></a>2.3.1 创建后置处理器类</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.process;<br>    <br><span class="hljs-keyword">import</span> org.springframework.beans.BeansException;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.config.BeanPostProcessor;<br>    <br><span class="hljs-comment">// 声明一个自定义的bean后置处理器</span><br><span class="hljs-comment">// 注意：bean后置处理器不是单独针对某一个bean生效，而是针对IOC容器中所有bean都会执行</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyHappyBeanProcessor</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">BeanPostProcessor</span> </span>&#123;<br>    <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">postProcessBeforeInitialization</span><span class="hljs-params">(Object bean, String beanName)</span> <span class="hljs-keyword">throws</span> BeansException </span>&#123;<br>    <br>        System.out.println(<span class="hljs-string">&quot;☆☆☆&quot;</span> + beanName + <span class="hljs-string">&quot; = &quot;</span> + bean);<br>    <br>        <span class="hljs-keyword">return</span> bean;<br>    &#125;<br>    <br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">postProcessAfterInitialization</span><span class="hljs-params">(Object bean, String beanName)</span> <span class="hljs-keyword">throws</span> BeansException </span>&#123;<br>    <br>        System.out.println(<span class="hljs-string">&quot;★★★&quot;</span> + beanName + <span class="hljs-string">&quot; = &quot;</span> + bean);<br>    <br>        <span class="hljs-keyword">return</span> bean;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-3-2-把bean的后置处理器放入IOC容器"><a href="#2-3-2-把bean的后置处理器放入IOC容器" class="headerlink" title="2.3.2 把bean的后置处理器放入IOC容器"></a>2.3.2 把bean的后置处理器放入IOC容器</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- bean的后置处理器要放入IOC容器才能生效 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;myHappyBeanProcessor&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.ioc.process.MyHappyBeanProcessor&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="2-3-3-执行效果示例"><a href="#2-3-3-执行效果示例" class="headerlink" title="2.3.3 执行效果示例"></a>2.3.3 执行效果示例</h6><blockquote>
<p>HappyComponent创建对象 </p>
<p>HappyComponent要设置属性了 </p>
<p>☆☆☆happyComponent = com.atguigu.ioc.component.HappyComponent@ca263c2 </p>
<p>HappyComponent初始化 </p>
<p>★★★happyComponent = com.atguigu.ioc.component.HappyComponent@ca263c2 HappyComponent销毁</p>
</blockquote>
<h3 id="第四节-FactoryBean机制-了解"><a href="#第四节-FactoryBean机制-了解" class="headerlink" title="第四节 FactoryBean机制(了解)"></a>第四节 FactoryBean机制(了解)</h3><h4 id="1-简介"><a href="#1-简介" class="headerlink" title="1. 简介"></a>1. 简介</h4><p>FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同，配置一个FactoryBean类型的bean，在获取bean的时候得到的并不是class属性中配置的这个类的对象，而是getObject()方法的返回值。通过这种机制，Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都屏蔽起来，只把最简洁的使用界面展示给我们。</p>
<p>将来我们整合Mybatis时，Spring就是通过FactoryBean机制来帮我们创建SqlSessionFactory对象的。</p>
<p>源码:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/*</span><br><span class="hljs-comment"> * Copyright 2002-2020 the original author or authors.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span><br><span class="hljs-comment"> * you may not use this file except in compliance with the License.</span><br><span class="hljs-comment"> * You may obtain a copy of the License at</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> *      https://www.apache.org/licenses/LICENSE-2.0</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * Unless required by applicable law or agreed to in writing, software</span><br><span class="hljs-comment"> * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span><br><span class="hljs-comment"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span><br><span class="hljs-comment"> * See the License for the specific language governing permissions and</span><br><span class="hljs-comment"> * limitations under the License.</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">package</span> org.springframework.beans.factory;<br><span class="hljs-keyword">import</span> org.springframework.lang.Nullable;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Interface to be implemented by objects used within a &#123;<span class="hljs-doctag">@link</span> BeanFactory&#125; which</span><br><span class="hljs-comment"> * are themselves factories for individual objects. If a bean implements this</span><br><span class="hljs-comment"> * interface, it is used as a factory for an object to expose, not directly as a</span><br><span class="hljs-comment"> * bean instance that will be exposed itself.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * &lt;p&gt;&lt;b&gt;NB: A bean that implements this interface cannot be used as a normal bean.&lt;/b&gt;</span><br><span class="hljs-comment"> * A FactoryBean is defined in a bean style, but the object exposed for bean</span><br><span class="hljs-comment"> * references (&#123;<span class="hljs-doctag">@link</span> #getObject()&#125;) is always the object that it creates.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * &lt;p&gt;FactoryBeans can support singletons and prototypes, and can either create</span><br><span class="hljs-comment"> * objects lazily on demand or eagerly on startup. The &#123;<span class="hljs-doctag">@link</span> SmartFactoryBean&#125;</span><br><span class="hljs-comment"> * interface allows for exposing more fine-grained behavioral metadata.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * &lt;p&gt;This interface is heavily used within the framework itself, for example for</span><br><span class="hljs-comment"> * the AOP &#123;<span class="hljs-doctag">@link</span> org.springframework.aop.framework.ProxyFactoryBean&#125; or the</span><br><span class="hljs-comment"> * &#123;<span class="hljs-doctag">@link</span> org.springframework.jndi.JndiObjectFactoryBean&#125;. It can be used for</span><br><span class="hljs-comment"> * custom components as well; however, this is only common for infrastructure code.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * &lt;p&gt;&lt;b&gt;&#123;<span class="hljs-doctag">@code</span> FactoryBean&#125; is a programmatic contract. Implementations are not</span><br><span class="hljs-comment"> * supposed to rely on annotation-driven injection or other reflective facilities.&lt;/b&gt;</span><br><span class="hljs-comment"> * &#123;<span class="hljs-doctag">@link</span> #getObjectType()&#125; &#123;<span class="hljs-doctag">@link</span> #getObject()&#125; invocations may arrive early in the</span><br><span class="hljs-comment"> * bootstrap process, even ahead of any post-processor setup. If you need access to</span><br><span class="hljs-comment"> * other beans, implement &#123;<span class="hljs-doctag">@link</span> BeanFactoryAware&#125; and obtain them programmatically.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * &lt;p&gt;&lt;b&gt;The container is only responsible for managing the lifecycle of the FactoryBean</span><br><span class="hljs-comment"> * instance, not the lifecycle of the objects created by the FactoryBean.&lt;/b&gt; Therefore,</span><br><span class="hljs-comment"> * a destroy method on an exposed bean object (such as &#123;<span class="hljs-doctag">@link</span> java.io.Closeable#close()&#125;</span><br><span class="hljs-comment"> * will &lt;i&gt;not&lt;/i&gt; be called automatically. Instead, a FactoryBean should implement</span><br><span class="hljs-comment"> * &#123;<span class="hljs-doctag">@link</span> DisposableBean&#125; and delegate any such close call to the underlying object.</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * &lt;p&gt;Finally, FactoryBean objects participate in the containing BeanFactory&#x27;s</span><br><span class="hljs-comment"> * synchronization of bean creation. There is usually no need for internal</span><br><span class="hljs-comment"> * synchronization other than for purposes of lazy initialization within the</span><br><span class="hljs-comment"> * FactoryBean itself (or the like).</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Rod Johnson</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Juergen Hoeller</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@since</span> 08.03.2003</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> &lt;T&gt; the bean type</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@see</span> org.springframework.beans.factory.BeanFactory</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@see</span> org.springframework.aop.framework.ProxyFactoryBean</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@see</span> org.springframework.jndi.JndiObjectFactoryBean</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">FactoryBean</span>&lt;<span class="hljs-title">T</span>&gt; </span>&#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * The name of an attribute that can be</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@link</span> org.springframework.core.AttributeAccessor#setAttribute set&#125; on a</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@link</span> org.springframework.beans.factory.config.BeanDefinition&#125; so that</span><br><span class="hljs-comment">     * factory beans can signal their object type when it can&#x27;t be deduced from</span><br><span class="hljs-comment">     * the factory bean class.</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@since</span> 5.2</span><br><span class="hljs-comment">     */</span><br>    String OBJECT_TYPE_ATTRIBUTE = <span class="hljs-string">&quot;factoryBeanObjectType&quot;</span>;<br>    <br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * Return an instance (possibly shared or independent) of the object</span><br><span class="hljs-comment">     * managed by this factory.</span><br><span class="hljs-comment">     * &lt;p&gt;As with a &#123;<span class="hljs-doctag">@link</span> BeanFactory&#125;, this allows support for both the</span><br><span class="hljs-comment">     * Singleton and Prototype design pattern.</span><br><span class="hljs-comment">     * &lt;p&gt;If this FactoryBean is not fully initialized yet at the time of</span><br><span class="hljs-comment">     * the call (for example because it is involved in a circular reference),</span><br><span class="hljs-comment">     * throw a corresponding &#123;<span class="hljs-doctag">@link</span> FactoryBeanNotInitializedException&#125;.</span><br><span class="hljs-comment">     * &lt;p&gt;As of Spring 2.0, FactoryBeans are allowed to return &#123;<span class="hljs-doctag">@code</span> null&#125;</span><br><span class="hljs-comment">     * objects. The factory will consider this as normal value to be used; it</span><br><span class="hljs-comment">     * will not throw a FactoryBeanNotInitializedException in this case anymore.</span><br><span class="hljs-comment">     * FactoryBean implementations are encouraged to throw</span><br><span class="hljs-comment">     * FactoryBeanNotInitializedException themselves now, as appropriate.</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> an instance of the bean (can be &#123;<span class="hljs-doctag">@code</span> null&#125;)</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception in case of creation errors</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@see</span> FactoryBeanNotInitializedException</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Nullable</span><br>    <span class="hljs-function">T <span class="hljs-title">getObject</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception</span>;<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * Return the type of object that this FactoryBean creates,</span><br><span class="hljs-comment">     * or &#123;<span class="hljs-doctag">@code</span> null&#125; if not known in advance.</span><br><span class="hljs-comment">     * &lt;p&gt;This allows one to check for specific types of beans without</span><br><span class="hljs-comment">     * instantiating objects, for example on autowiring.</span><br><span class="hljs-comment">     * &lt;p&gt;In the case of implementations that are creating a singleton object,</span><br><span class="hljs-comment">     * this method should try to avoid singleton creation as far as possible;</span><br><span class="hljs-comment">     * it should rather estimate the type in advance.</span><br><span class="hljs-comment">     * For prototypes, returning a meaningful type here is advisable too.</span><br><span class="hljs-comment">     * &lt;p&gt;This method can be called &lt;i&gt;before&lt;/i&gt; this FactoryBean has</span><br><span class="hljs-comment">     * been fully initialized. It must not rely on state created during</span><br><span class="hljs-comment">     * initialization; of course, it can still use such state if available.</span><br><span class="hljs-comment">     * &lt;p&gt;&lt;b&gt;<span class="hljs-doctag">NOTE:</span>&lt;/b&gt; Autowiring will simply ignore FactoryBeans that return</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@code</span> null&#125; here. Therefore it is highly recommended to implement</span><br><span class="hljs-comment">     * this method properly, using the current state of the FactoryBean.</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> the type of object that this FactoryBean creates,</span><br><span class="hljs-comment">     * or &#123;<span class="hljs-doctag">@code</span> null&#125; if not known at the time of the call</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@see</span> ListableBeanFactory#getBeansOfType</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Nullable</span><br>    Class&lt;?&gt; getObjectType();<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * Is the object managed by this factory a singleton? That is,</span><br><span class="hljs-comment">     * will &#123;<span class="hljs-doctag">@link</span> #getObject()&#125; always return the same object</span><br><span class="hljs-comment">     * (a reference that can be cached)?</span><br><span class="hljs-comment">     * &lt;p&gt;&lt;b&gt;<span class="hljs-doctag">NOTE:</span>&lt;/b&gt; If a FactoryBean indicates to hold a singleton object,</span><br><span class="hljs-comment">     * the object returned from &#123;<span class="hljs-doctag">@code</span> getObject()&#125; might get cached</span><br><span class="hljs-comment">     * by the owning BeanFactory. Hence, do not return &#123;<span class="hljs-doctag">@code</span> true&#125;</span><br><span class="hljs-comment">     * unless the FactoryBean always exposes the same reference.</span><br><span class="hljs-comment">     * &lt;p&gt;The singleton status of the FactoryBean itself will generally</span><br><span class="hljs-comment">     * be provided by the owning BeanFactory; usually, it has to be</span><br><span class="hljs-comment">     * defined as singleton there.</span><br><span class="hljs-comment">     * &lt;p&gt;&lt;b&gt;<span class="hljs-doctag">NOTE:</span>&lt;/b&gt; This method returning &#123;<span class="hljs-doctag">@code</span> false&#125; does not</span><br><span class="hljs-comment">     * necessarily indicate that returned objects are independent instances.</span><br><span class="hljs-comment">     * An implementation of the extended &#123;<span class="hljs-doctag">@link</span> SmartFactoryBean&#125; interface</span><br><span class="hljs-comment">     * may explicitly indicate independent instances through its</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@link</span> SmartFactoryBean#isPrototype()&#125; method. Plain &#123;<span class="hljs-doctag">@link</span> FactoryBean&#125;</span><br><span class="hljs-comment">     * implementations which do not implement this extended interface are</span><br><span class="hljs-comment">     * simply assumed to always return independent instances if the</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@code</span> isSingleton()&#125; implementation returns &#123;<span class="hljs-doctag">@code</span> false&#125;.</span><br><span class="hljs-comment">     * &lt;p&gt;The default implementation returns &#123;<span class="hljs-doctag">@code</span> true&#125;, since a</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@code</span> FactoryBean&#125; typically manages a singleton instance.</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> whether the exposed object is a singleton</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@see</span> #getObject()</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@see</span> SmartFactoryBean#isPrototype()</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">default</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isSingleton</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="2-实现FactoryBean接口"><a href="#2-实现FactoryBean接口" class="headerlink" title="2. 实现FactoryBean接口"></a>2. 实现FactoryBean接口</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-keyword">import</span> org.springframework.beans.factory.FactoryBean;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.component</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"> * 日期2021-08-31  09:03</span><br><span class="hljs-comment"> * 要使用FactoryBean机制就得写一个类实现FactoryBean接口,接口的泛型表示你想通过这个FactoryBean创建什么对象</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * FactoryBean机制:你在spring的配置文件中进行IOC配置的是HappyComponentFactoryBean类，但是真正创建出来存储在核心容器中的对象是</span><br><span class="hljs-comment"> *                HappyComponentFactoryBean对象调用getObject()方法所获取的对象(HappyComponent)</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponentFactoryBean</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">FactoryBean</span>&lt;<span class="hljs-title">HappyComponent</span>&gt; </span>&#123;<br>    <span class="hljs-keyword">private</span> String componentName;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setComponentName</span><span class="hljs-params">(String componentName)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.componentName = componentName;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> HappyComponent <span class="hljs-title">getObject</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception </span>&#123;<br>        HappyComponent happyComponent = <span class="hljs-keyword">new</span> HappyComponent();<br>        happyComponent.setComponentName(componentName);<br>        <span class="hljs-keyword">return</span> happyComponent;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> Class&lt;?&gt; getObjectType() &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="3-配置bean"><a href="#3-配置bean" class="headerlink" title="3. 配置bean"></a>3. 配置bean</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;happyComponent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.atguigu.component.HappyComponentFactoryBean&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;componentName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;奥巴马&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="4-测试获取bean"><a href="#4-测试获取bean" class="headerlink" title="4. 测试获取bean"></a>4. 测试获取bean</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu;<br><br><span class="hljs-keyword">import</span> com.atguigu.component.HappyComponent;<br><span class="hljs-keyword">import</span> org.junit.Test;<br><span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;<br><span class="hljs-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;<br><br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestFactoryBean</span> </span>&#123;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testGetBean</span><span class="hljs-params">()</span></span>&#123;<br>        <span class="hljs-comment">//1. 创建核心容器</span><br>        ApplicationContext act = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">&quot;spring-application.xml&quot;</span>);<br>        <span class="hljs-comment">//2.从核心容器中获取对象</span><br>        HappyComponent happyComponent = (HappyComponent) act.getBean(<span class="hljs-string">&quot;happyComponent&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/spring/">spring</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/spring/">spring</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客目前大部分文章都是参考尚硅谷或者马士兵教育的学习资料！<a href="http://www.atguigu.com/" rel="nofollow noopener"官网地址！</a> 
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/06/%E5%AD%97%E7%AC%A6%E9%9B%86%E5%92%8C%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">字符集和编码问题</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/03/IDEA_Import_SpringCode/">
                        <span class="hidden-mobile">IDEA导入Spring源码</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">

  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('11/02/2021 21:39:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
